package com.inspur.gs.sbssdl.sbwhgl.bpbjgl.bpbjgl.back.bizactions;
import com.inspur.edp.bef.api.lcp.ILcpFactory;
import com.inspur.edp.bef.api.lcp.IStandardLcp;
import com.inspur.edp.bef.api.services.IBefSessionManager;
import com.inspur.edp.bef.engine.entity.AssociationInfo;
import com.inspur.edp.cef.api.message.*;
import com.inspur.edp.bef.api.action.determination.*;
import com.inspur.edp.bef.api.action.validation.*;
import com.inspur.edp.cef.entity.changeset.*;
import com.inspur.edp.bef.api.be.*;
import com.inspur.edp.bef.api.action.VoidActionResult;
import com.inspur.edp.bef.spi.action.AbstractManagerAction;
import com.inspur.edp.cef.entity.entity.IEntityData;
import com.inspur.edp.cef.entity.entity.IEntityDataCollection;
import com.inspur.edp.cef.entity.entity.IValuesContainer;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class bpbjckglbpbjckWriteBackMgrAction extends AbstractManagerAction<VoidActionResult> {
	private String dataid;
	private String jlz;
	public bpbjckglbpbjckWriteBackMgrAction(IBEManagerContext managerContext, String dataid, String jlz) {
		super(managerContext);
		this.dataid = dataid;
		this.jlz = jlz;
	}
	@Override
	public  void execute() {
		/**
		 * 出库回写
		 * 1.减去对应台账的数量
		 * 2.新增一条变更记录
		 */
		if (jlz.equals("back")){
			return;
		}
		SpringBeanUtils.getBean(IBefSessionManager.class).createSession();
		try {
			ILcpFactory iLcpFactory = SpringBeanUtils.getBean(ILcpFactory.class);
			String ckid = "e8d0afeb-df0e-4b60-be42-fe2fa9cd54e0";
			String tzid = "3f443799-4bc1-409f-a28d-f04742273899";
			IStandardLcp cklcp = iLcpFactory.createLcpByBEId(ckid);
			IStandardLcp tzlcp = iLcpFactory.createLcpByBEId(tzid);

			IEntityData data = cklcp.retrieve(dataid).getData();
			String ckbm = ((IValuesContainer) data.getValue("ckbm")).getValue("ckbm").toString();
			IEntityDataCollection ckmxList = cklcp.retrieve(dataid).getData().getChilds().get("ckmx");
			ckmxList.stream().forEach(ckmx->{
				String wzlxid = ((IValuesContainer)ckmx.getValue("wzlxid")).getValue("wzlxid").toString();
				BigDecimal sl = (BigDecimal) ckmx.getValue("sl");
				BigDecimal lksl = (BigDecimal) tzlcp.retrieve(wzlxid).getData().getValue("lksl");
				ModifyChangeDetail changeDetail = new ModifyChangeDetail(wzlxid);
				changeDetail.getPropertyChanges().put("lksl", lksl.divide(sl));
				//子表关联
				ModifyChangeDetail childChange = new ModifyChangeDetail(wzlxid);
				childChange.getPropertyChanges().put("bglx", "出库");
				childChange.getPropertyChanges().put("sl", sl);
				AssociationInfo bgbmInfo  = new AssociationInfo();
				bgbmInfo.setValue("bgbm",ckbm);
				childChange.getPropertyChanges().put("bgbm", bgbmInfo);
				SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
				try {
					Date parse = formatter.parse(formatter.format(new Date()));
					childChange.getPropertyChanges().put("bgsj",parse);
				} catch (ParseException e) {
					throw new RuntimeException(e);
				}
				changeDetail.addChildChangeSet("bgjl", childChange);
				tzlcp.modify(changeDetail);
				tzlcp.save();
			});
		} finally {
			SpringBeanUtils.getBean(IBefSessionManager.class).closeCurrentSession();
		}
	}
	private IBusinessEntity getEntity(String dataId) {
		return (IBusinessEntity)super.getBEManagerContext().getEntity(dataId);
	}	private IBEService getMgr() {
		return (IBEService)super.getBEManagerContext().getBEManager();
	}
}